{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1842afe4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install autopep8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e9ba0657",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import pickle\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "85886e36",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install pandas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "35676788",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"ss2109_feature.pkl\",\"rb\") as f:\n",
    "    data=pickle.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d3c4bda0",
   "metadata": {},
   "outputs": [],
   "source": [
    "raw_data=data['raw_data']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d43dec87",
   "metadata": {},
   "outputs": [],
   "source": [
    "feature=data['feature']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "01edd785",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                    infotime InstrumentID    Price  Volume      Amount  \\\n",
      "0      21-08-04 21:00:01.356       ss2109  18895.0    43.0   4062775.0   \n",
      "1      21-08-04 21:00:01.857       ss2109  18900.0   128.0  12095550.0   \n",
      "2      21-08-04 21:00:02.360       ss2109  18905.0    26.0   2457525.0   \n",
      "3      21-08-04 21:00:02.856       ss2109  18910.0    27.0   2553000.0   \n",
      "4      21-08-04 21:00:03.357       ss2109  18910.0    24.0   2269425.0   \n",
      "...                      ...          ...      ...     ...         ...   \n",
      "48987  21-08-05 15:00:00.304       ss2109  18785.0     4.0    375625.0   \n",
      "48988  21-08-05 15:00:00.811       ss2109  18780.0    18.0   1690375.0   \n",
      "48989  21-08-05 15:00:01.306       ss2109  18780.0     0.0         0.0   \n",
      "48990  21-08-05 15:00:01.798       ss2109  18785.0     2.0    187875.0   \n",
      "48991  21-08-05 15:00:02.319       ss2109  18785.0     3.0    281775.0   \n",
      "\n",
      "           BP1      BP2      BP3      BP4      BP5  ...      SP4      SP5  \\\n",
      "0      18895.0  18890.0  18885.0  18880.0  18870.0  ...  18920.0  18930.0   \n",
      "1      18900.0  18895.0  18890.0  18885.0  18880.0  ...  18935.0  18940.0   \n",
      "2      18905.0  18900.0  18895.0  18890.0  18885.0  ...  18930.0  18935.0   \n",
      "3      18910.0  18905.0  18900.0  18895.0  18890.0  ...  18935.0  18940.0   \n",
      "4      18910.0  18905.0  18900.0  18895.0  18890.0  ...  18935.0  18940.0   \n",
      "...        ...      ...      ...      ...      ...  ...      ...      ...   \n",
      "48987  18780.0  18775.0  18770.0  18765.0  18760.0  ...  18800.0  18805.0   \n",
      "48988  18780.0  18775.0  18770.0  18765.0  18760.0  ...  18805.0  18810.0   \n",
      "48989  18785.0  18780.0  18775.0  18770.0  18765.0  ...  18805.0  18810.0   \n",
      "48990  18785.0  18780.0  18775.0  18770.0  18765.0  ...  18810.0  18815.0   \n",
      "48991  18785.0  18780.0  18775.0  18770.0  18765.0  ...  18810.0  18815.0   \n",
      "\n",
      "       SV1  SV2  SV3  SV4  SV5  UpdateTime  UpdateMillisec  realPrice  \n",
      "0        1    6    1    2    1    21:00:00             500    18896.6  \n",
      "1        2    2    2   10   11    21:00:01               0    18899.3  \n",
      "2        6   18   25    2   10    21:00:01             500    18904.0  \n",
      "3       16   25    2   10   12    21:00:02               0    18911.1  \n",
      "4        9   25    3   15   10    21:00:02             500    18911.9  \n",
      "...    ...  ...  ...  ...  ...         ...             ...        ...  \n",
      "48987    3    3    1   29   12    14:59:58               0    18781.2  \n",
      "48988    1    1   30   12   45    14:59:58             500    18781.9  \n",
      "48989    1    1   31   12   45    14:59:59               0        NaN  \n",
      "48990    1   31   12   45   14    14:59:59             500    18787.5  \n",
      "48991    1   31   12   45   14    15:00:00               0    18785.0  \n",
      "\n",
      "[48992 rows x 28 columns]\n"
     ]
    }
   ],
   "source": [
    "# raw_data['UpdateTime'].str.split(\":\")\n",
    "print(raw_data)\n",
    "# raw_data=raw_data.set_index(\"infotime\")\n",
    "\n",
    "# raw_data.index=pd.to_datetime(raw_data.index)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9da13d4c",
   "metadata": {},
   "source": [
    "```\n",
    "wfea3:active_buyer判断卖单\n",
    "wfea4:active_seller判断买单\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "5bd1b369",
   "metadata": {},
   "outputs": [],
   "source": [
    "raw_data.index=pd.to_datetime((raw_data[\"infotime\"].str[0:9]+raw_data['UpdateTime']+\".\"+raw_data['UpdateMillisec'].astype(str)).values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "10a77ae5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>infotime</th>\n",
       "      <th>InstrumentID</th>\n",
       "      <th>Price</th>\n",
       "      <th>Volume</th>\n",
       "      <th>Amount</th>\n",
       "      <th>BP1</th>\n",
       "      <th>BP2</th>\n",
       "      <th>BP3</th>\n",
       "      <th>BP4</th>\n",
       "      <th>BP5</th>\n",
       "      <th>...</th>\n",
       "      <th>SP4</th>\n",
       "      <th>SP5</th>\n",
       "      <th>SV1</th>\n",
       "      <th>SV2</th>\n",
       "      <th>SV3</th>\n",
       "      <th>SV4</th>\n",
       "      <th>SV5</th>\n",
       "      <th>UpdateTime</th>\n",
       "      <th>UpdateMillisec</th>\n",
       "      <th>realPrice</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2004-08-21 21:00:00.500</th>\n",
       "      <td>21-08-04 21:00:01.356</td>\n",
       "      <td>ss2109</td>\n",
       "      <td>18895.0</td>\n",
       "      <td>43.0</td>\n",
       "      <td>4062775.0</td>\n",
       "      <td>18895.0</td>\n",
       "      <td>18890.0</td>\n",
       "      <td>18885.0</td>\n",
       "      <td>18880.0</td>\n",
       "      <td>18870.0</td>\n",
       "      <td>...</td>\n",
       "      <td>18920.0</td>\n",
       "      <td>18930.0</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>21:00:00</td>\n",
       "      <td>500</td>\n",
       "      <td>18896.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-08-21 21:00:01.000</th>\n",
       "      <td>21-08-04 21:00:01.857</td>\n",
       "      <td>ss2109</td>\n",
       "      <td>18900.0</td>\n",
       "      <td>128.0</td>\n",
       "      <td>12095550.0</td>\n",
       "      <td>18900.0</td>\n",
       "      <td>18895.0</td>\n",
       "      <td>18890.0</td>\n",
       "      <td>18885.0</td>\n",
       "      <td>18880.0</td>\n",
       "      <td>...</td>\n",
       "      <td>18935.0</td>\n",
       "      <td>18940.0</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "      <td>21:00:01</td>\n",
       "      <td>0</td>\n",
       "      <td>18899.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-08-21 21:00:01.500</th>\n",
       "      <td>21-08-04 21:00:02.360</td>\n",
       "      <td>ss2109</td>\n",
       "      <td>18905.0</td>\n",
       "      <td>26.0</td>\n",
       "      <td>2457525.0</td>\n",
       "      <td>18905.0</td>\n",
       "      <td>18900.0</td>\n",
       "      <td>18895.0</td>\n",
       "      <td>18890.0</td>\n",
       "      <td>18885.0</td>\n",
       "      <td>...</td>\n",
       "      <td>18930.0</td>\n",
       "      <td>18935.0</td>\n",
       "      <td>6</td>\n",
       "      <td>18</td>\n",
       "      <td>25</td>\n",
       "      <td>2</td>\n",
       "      <td>10</td>\n",
       "      <td>21:00:01</td>\n",
       "      <td>500</td>\n",
       "      <td>18904.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-08-21 21:00:02.000</th>\n",
       "      <td>21-08-04 21:00:02.856</td>\n",
       "      <td>ss2109</td>\n",
       "      <td>18910.0</td>\n",
       "      <td>27.0</td>\n",
       "      <td>2553000.0</td>\n",
       "      <td>18910.0</td>\n",
       "      <td>18905.0</td>\n",
       "      <td>18900.0</td>\n",
       "      <td>18895.0</td>\n",
       "      <td>18890.0</td>\n",
       "      <td>...</td>\n",
       "      <td>18935.0</td>\n",
       "      <td>18940.0</td>\n",
       "      <td>16</td>\n",
       "      <td>25</td>\n",
       "      <td>2</td>\n",
       "      <td>10</td>\n",
       "      <td>12</td>\n",
       "      <td>21:00:02</td>\n",
       "      <td>0</td>\n",
       "      <td>18911.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-08-21 21:00:02.500</th>\n",
       "      <td>21-08-04 21:00:03.357</td>\n",
       "      <td>ss2109</td>\n",
       "      <td>18910.0</td>\n",
       "      <td>24.0</td>\n",
       "      <td>2269425.0</td>\n",
       "      <td>18910.0</td>\n",
       "      <td>18905.0</td>\n",
       "      <td>18900.0</td>\n",
       "      <td>18895.0</td>\n",
       "      <td>18890.0</td>\n",
       "      <td>...</td>\n",
       "      <td>18935.0</td>\n",
       "      <td>18940.0</td>\n",
       "      <td>9</td>\n",
       "      <td>25</td>\n",
       "      <td>3</td>\n",
       "      <td>15</td>\n",
       "      <td>10</td>\n",
       "      <td>21:00:02</td>\n",
       "      <td>500</td>\n",
       "      <td>18911.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2005-08-21 14:59:58.000</th>\n",
       "      <td>21-08-05 15:00:00.304</td>\n",
       "      <td>ss2109</td>\n",
       "      <td>18785.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>375625.0</td>\n",
       "      <td>18780.0</td>\n",
       "      <td>18775.0</td>\n",
       "      <td>18770.0</td>\n",
       "      <td>18765.0</td>\n",
       "      <td>18760.0</td>\n",
       "      <td>...</td>\n",
       "      <td>18800.0</td>\n",
       "      <td>18805.0</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>29</td>\n",
       "      <td>12</td>\n",
       "      <td>14:59:58</td>\n",
       "      <td>0</td>\n",
       "      <td>18781.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2005-08-21 14:59:58.500</th>\n",
       "      <td>21-08-05 15:00:00.811</td>\n",
       "      <td>ss2109</td>\n",
       "      <td>18780.0</td>\n",
       "      <td>18.0</td>\n",
       "      <td>1690375.0</td>\n",
       "      <td>18780.0</td>\n",
       "      <td>18775.0</td>\n",
       "      <td>18770.0</td>\n",
       "      <td>18765.0</td>\n",
       "      <td>18760.0</td>\n",
       "      <td>...</td>\n",
       "      <td>18805.0</td>\n",
       "      <td>18810.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>30</td>\n",
       "      <td>12</td>\n",
       "      <td>45</td>\n",
       "      <td>14:59:58</td>\n",
       "      <td>500</td>\n",
       "      <td>18781.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2005-08-21 14:59:59.000</th>\n",
       "      <td>21-08-05 15:00:01.306</td>\n",
       "      <td>ss2109</td>\n",
       "      <td>18780.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>18785.0</td>\n",
       "      <td>18780.0</td>\n",
       "      <td>18775.0</td>\n",
       "      <td>18770.0</td>\n",
       "      <td>18765.0</td>\n",
       "      <td>...</td>\n",
       "      <td>18805.0</td>\n",
       "      <td>18810.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>31</td>\n",
       "      <td>12</td>\n",
       "      <td>45</td>\n",
       "      <td>14:59:59</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2005-08-21 14:59:59.500</th>\n",
       "      <td>21-08-05 15:00:01.798</td>\n",
       "      <td>ss2109</td>\n",
       "      <td>18785.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>187875.0</td>\n",
       "      <td>18785.0</td>\n",
       "      <td>18780.0</td>\n",
       "      <td>18775.0</td>\n",
       "      <td>18770.0</td>\n",
       "      <td>18765.0</td>\n",
       "      <td>...</td>\n",
       "      <td>18810.0</td>\n",
       "      <td>18815.0</td>\n",
       "      <td>1</td>\n",
       "      <td>31</td>\n",
       "      <td>12</td>\n",
       "      <td>45</td>\n",
       "      <td>14</td>\n",
       "      <td>14:59:59</td>\n",
       "      <td>500</td>\n",
       "      <td>18787.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2005-08-21 15:00:00.000</th>\n",
       "      <td>21-08-05 15:00:02.319</td>\n",
       "      <td>ss2109</td>\n",
       "      <td>18785.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>281775.0</td>\n",
       "      <td>18785.0</td>\n",
       "      <td>18780.0</td>\n",
       "      <td>18775.0</td>\n",
       "      <td>18770.0</td>\n",
       "      <td>18765.0</td>\n",
       "      <td>...</td>\n",
       "      <td>18810.0</td>\n",
       "      <td>18815.0</td>\n",
       "      <td>1</td>\n",
       "      <td>31</td>\n",
       "      <td>12</td>\n",
       "      <td>45</td>\n",
       "      <td>14</td>\n",
       "      <td>15:00:00</td>\n",
       "      <td>0</td>\n",
       "      <td>18785.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>48992 rows × 28 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                      infotime InstrumentID    Price  Volume  \\\n",
       "2004-08-21 21:00:00.500  21-08-04 21:00:01.356       ss2109  18895.0    43.0   \n",
       "2004-08-21 21:00:01.000  21-08-04 21:00:01.857       ss2109  18900.0   128.0   \n",
       "2004-08-21 21:00:01.500  21-08-04 21:00:02.360       ss2109  18905.0    26.0   \n",
       "2004-08-21 21:00:02.000  21-08-04 21:00:02.856       ss2109  18910.0    27.0   \n",
       "2004-08-21 21:00:02.500  21-08-04 21:00:03.357       ss2109  18910.0    24.0   \n",
       "...                                        ...          ...      ...     ...   \n",
       "2005-08-21 14:59:58.000  21-08-05 15:00:00.304       ss2109  18785.0     4.0   \n",
       "2005-08-21 14:59:58.500  21-08-05 15:00:00.811       ss2109  18780.0    18.0   \n",
       "2005-08-21 14:59:59.000  21-08-05 15:00:01.306       ss2109  18780.0     0.0   \n",
       "2005-08-21 14:59:59.500  21-08-05 15:00:01.798       ss2109  18785.0     2.0   \n",
       "2005-08-21 15:00:00.000  21-08-05 15:00:02.319       ss2109  18785.0     3.0   \n",
       "\n",
       "                             Amount      BP1      BP2      BP3      BP4  \\\n",
       "2004-08-21 21:00:00.500   4062775.0  18895.0  18890.0  18885.0  18880.0   \n",
       "2004-08-21 21:00:01.000  12095550.0  18900.0  18895.0  18890.0  18885.0   \n",
       "2004-08-21 21:00:01.500   2457525.0  18905.0  18900.0  18895.0  18890.0   \n",
       "2004-08-21 21:00:02.000   2553000.0  18910.0  18905.0  18900.0  18895.0   \n",
       "2004-08-21 21:00:02.500   2269425.0  18910.0  18905.0  18900.0  18895.0   \n",
       "...                             ...      ...      ...      ...      ...   \n",
       "2005-08-21 14:59:58.000    375625.0  18780.0  18775.0  18770.0  18765.0   \n",
       "2005-08-21 14:59:58.500   1690375.0  18780.0  18775.0  18770.0  18765.0   \n",
       "2005-08-21 14:59:59.000         0.0  18785.0  18780.0  18775.0  18770.0   \n",
       "2005-08-21 14:59:59.500    187875.0  18785.0  18780.0  18775.0  18770.0   \n",
       "2005-08-21 15:00:00.000    281775.0  18785.0  18780.0  18775.0  18770.0   \n",
       "\n",
       "                             BP5  ...      SP4      SP5  SV1  SV2  SV3  SV4  \\\n",
       "2004-08-21 21:00:00.500  18870.0  ...  18920.0  18930.0    1    6    1    2   \n",
       "2004-08-21 21:00:01.000  18880.0  ...  18935.0  18940.0    2    2    2   10   \n",
       "2004-08-21 21:00:01.500  18885.0  ...  18930.0  18935.0    6   18   25    2   \n",
       "2004-08-21 21:00:02.000  18890.0  ...  18935.0  18940.0   16   25    2   10   \n",
       "2004-08-21 21:00:02.500  18890.0  ...  18935.0  18940.0    9   25    3   15   \n",
       "...                          ...  ...      ...      ...  ...  ...  ...  ...   \n",
       "2005-08-21 14:59:58.000  18760.0  ...  18800.0  18805.0    3    3    1   29   \n",
       "2005-08-21 14:59:58.500  18760.0  ...  18805.0  18810.0    1    1   30   12   \n",
       "2005-08-21 14:59:59.000  18765.0  ...  18805.0  18810.0    1    1   31   12   \n",
       "2005-08-21 14:59:59.500  18765.0  ...  18810.0  18815.0    1   31   12   45   \n",
       "2005-08-21 15:00:00.000  18765.0  ...  18810.0  18815.0    1   31   12   45   \n",
       "\n",
       "                         SV5  UpdateTime  UpdateMillisec  realPrice  \n",
       "2004-08-21 21:00:00.500    1    21:00:00             500    18896.6  \n",
       "2004-08-21 21:00:01.000   11    21:00:01               0    18899.3  \n",
       "2004-08-21 21:00:01.500   10    21:00:01             500    18904.0  \n",
       "2004-08-21 21:00:02.000   12    21:00:02               0    18911.1  \n",
       "2004-08-21 21:00:02.500   10    21:00:02             500    18911.9  \n",
       "...                      ...         ...             ...        ...  \n",
       "2005-08-21 14:59:58.000   12    14:59:58               0    18781.2  \n",
       "2005-08-21 14:59:58.500   45    14:59:58             500    18781.9  \n",
       "2005-08-21 14:59:59.000   45    14:59:59               0        NaN  \n",
       "2005-08-21 14:59:59.500   14    14:59:59             500    18787.5  \n",
       "2005-08-21 15:00:00.000   14    15:00:00               0    18785.0  \n",
       "\n",
       "[48992 rows x 28 columns]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "raw_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "aa0be9b6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting backtesting_demo.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile backtesting_demo.py\n",
    "#!/usr/bin/env python\n",
    "# coding: utf-8\n",
    "\n",
    "# In[20]:\n",
    "\n",
    "\n",
    "#%%\n",
    "from vnpy.app.cta_strategy.backtesting import BacktestingEngine, OptimizationSetting\n",
    "from vnpy.app.cta_strategy.strategies.high_frequency_1 import (\n",
    "    MultiTimeframeStrategy,\n",
    ")\n",
    "from vnpy.app.cta_strategy.base import BacktestingMode\n",
    "# from vnpy.app.cta_strategy.strategies.atr_rsi_strategy import AtrRsiStrategy\n",
    "from datetime import datetime\n",
    "import concurrent.futures\n",
    "import multiprocessing\n",
    "import pandas as pd\n",
    "# In[21]:\n",
    "\n",
    "import copy\n",
    "#%%\n",
    "def backtest(symbol):\n",
    "    engine = BacktestingEngine()\n",
    "    engine.set_parameters(\n",
    "        vt_symbol=\"{}.LOCAL\".format(symbol),\n",
    "        interval=\"1m\",\n",
    "        start=datetime(2021, 7, 1),\n",
    "        end=datetime(2021, 8, 6),\n",
    "        rate=2/10000,\n",
    "        slippage=0,\n",
    "        size=1,\n",
    "        pricetick=0.2,\n",
    "        capital=20_000,\n",
    "        mode=BacktestingMode.TICK\n",
    "    )\n",
    "\n",
    "    engine.add_strategy(MultiTimeframeStrategy, {})\n",
    "\n",
    "\n",
    "    # In[22]:\n",
    "\n",
    "\n",
    "    #%%\n",
    "    engine.load_data()\n",
    "    engine.run_backtesting()\n",
    "\n",
    "\n",
    "    return symbol,engine\n",
    "def backtest2(engine):\n",
    "    return engine.calculate_result(),engine.logs\n",
    "    \n",
    "if __name__ == '__main__':\n",
    "    # 衍生信号\n",
    "    __spec__ = \"ModuleSpec(name='builtins', loader=<class '_frozen_importlib.BuiltinImporter'>)\"\n",
    "\n",
    "    engines={}\n",
    "#     symbols=['AP', 'CJ', 'FU', 'EB', 'EG', 'CS', 'CF', 'HC', 'BU', 'JD', 'AL', 'CU', 'I', 'J']+[ 'JM', 'MA', 'M', 'OI', 'P', 'PP', 'RB', 'NI', 'SF', 'RU']+['RM', 'SM', 'PB', 'SA', 'SP', 'UR', 'SR', 'TA', 'ZC', 'V', 'Y', 'SS', 'ZN']\n",
    "    symbols=['ss']\n",
    "    df=pd.DataFrame()\n",
    "    logs=[]\n",
    "    with concurrent.futures.ProcessPoolExecutor(\n",
    "            max_workers=multiprocessing.cpu_count() - 1) as executor:\n",
    "        futures = [\n",
    "            executor.submit(backtest,symbol)\n",
    "            for symbol in symbols\n",
    "        ]\n",
    "\n",
    "        for future in concurrent.futures.as_completed(futures):\n",
    "            symbol,engine=future.result()\n",
    "            engines[symbol]=engine\n",
    "\n",
    "            if future.exception():\n",
    "                print(future.exception())\n",
    "    with concurrent.futures.ProcessPoolExecutor(\n",
    "            max_workers=multiprocessing.cpu_count() - 1) as executor2:\n",
    "\n",
    "        # print(symbols)\n",
    "    \n",
    "        futures2 = [\n",
    "            executor2.submit(backtest2,engine)\n",
    "            for engine in engines.values()\n",
    "        ]\n",
    "        \n",
    "        for future2 in concurrent.futures.as_completed(futures2):\n",
    "            df_,logs_=future2.result()\n",
    "            df=pd.concat((df,df_))\n",
    "            logs.extend(logs_)\n",
    "            if future2.exception():\n",
    "                print(future2.exception())\n",
    "        \n",
    "\n",
    "    \n",
    "\n",
    "\n",
    "# In[13]:\n",
    "\n",
    "\n",
    "# setting = OptimizationSetting()\n",
    "# setting.set_target(\"sharpe_ratio\")\n",
    "# setting.add_parameter(\"atr_length\", 3, 39, 1)\n",
    "# setting.add_parameter(\"atr_ma_length\", 10, 30, 1)\n",
    "\n",
    "# engine.run_ga_optimization(setting)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1972010e",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\anaconda3\\envs\\vnpy_backtest\\lib\\site-packages\\deap\\creator.py:141: RuntimeWarning: A class named 'FitnessMax' has already been created and it will be overwritten. Consider deleting previous creation of that class or rename it.\n",
      "  RuntimeWarning)\n",
      "d:\\anaconda3\\envs\\vnpy_backtest\\lib\\site-packages\\deap\\creator.py:141: RuntimeWarning: A class named 'Individual' has already been created and it will be overwritten. Consider deleting previous creation of that class or rename it.\n",
      "  RuntimeWarning)\n"
     ]
    }
   ],
   "source": [
    "%run backtesting_demo.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e6e3deac",
   "metadata": {},
   "outputs": [],
   "source": [
    "import copy\n",
    "#     print(df)\n",
    "# print(symbol)\n",
    "df=df.sort_values(by=\"date\")\n",
    "# print(df)\n",
    "df=df.groupby(\"date\").sum()\n",
    "# print(df)\n",
    "df=df[[\"trade_count\",\"turnover\",\"commission\",\"slippage\",\"trading_pnl\",\"holding_pnl\",\"total_pnl\",\"net_pnl\"]]\n",
    "engine=copy.deepcopy(engines[symbol])\n",
    "engine.capital=20_000*len(symbols)\n",
    "engine.calculate_statistics(df,output=True)\n",
    "# print(\"--\"*63)\n",
    "# print(\"\\t\\t\\t\\t\\t\\t\\t\",symbol)\n",
    "engine.show_chart(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d81ade73",
   "metadata": {},
   "outputs": [],
   "source": [
    "logs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85c44b0d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad405d31",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "682613eaa3148413377cb622a4189a88245f7cd48bab4f15a70c02a4e9773461"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
